import {
	requestNoAuth,
	request
} from '@/lib/java110/java110Request.js';
import url from '@/constant/url.js';

import {getUserId} from '@/api/user/userApi.js';

import conf from '@/conf/config.js';

// #ifdef H5
const WexinPayFactory = require('../../lib/java110/factory/WexinPayFactory.js');
// #endif


export function refreshUserOpenId(_objData){
	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.refreshOpenId,
			method: "GET",
			data: _objData, //动态数据
			success: function(res) {
				if (res.statusCode == 200) {
					resolve(res.data);
					return;
				}
				reject();
			},
			fail: function(e) {
				reject();
			}
		});
	})
}



export function getWechatMiniOpenId(_objData){
	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.getOpenIdByCode,
			method: "GET",
			data: _objData, //动态数据
			success: function(res) {
				if (res.statusCode == 200) {
					resolve(res.data);
					return;
				}
				reject();
			},
			fail: function(e) {
				reject();
			}
		});
	})
}
/**
 * 刷新用户
 * @param {Object} _objData
 */
export function getOpenIdFromAliPay(_objData){
	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.getOpenIdFromAliPay,
			method: "GET",
			data: _objData, //动态数据
			success: function(res) {
				if (res.statusCode == 200) {
					resolve(res.data);
					return;
				}
				reject();
			},
			fail: function(e) {
				reject();
			}
		});
	})
}


/**
 * 收银台 支付
 * @param {Object} _that
 * @param {Object} _data
 * @param {Object} _successUrl
 */
export function cashierPayFee(_that, _data,_successUrl) {
	if(!_successUrl ){
		_successUrl = "/pages/order/order";
	}
	wx.showLoading({
		title: '支付中'
	});
	requestNoAuth({
		url: url.cashier,
		method: "POST",
		data: _data,
		//动态数据
		success: function(res) {
			wx.hideLoading();
			if (res.data.code == '0') {
				let data = res.data; //成功情况下跳转
				// #ifdef MP-WEIXIN
				uni.requestPayment({
					'timeStamp': data.timeStamp,
					'nonceStr': data.nonceStr,
					'package': data.package,
					'signType': data.signType,
					'paySign': data.sign,
					'success': function(res) {
						uni.navigateTo({
							url: _successUrl
						});
					},
					'fail': function(res) {
						_that.banButton = false;
					}
				});
				// #endif
				// #ifdef H5
				WexinPayFactory.wexinPay(data, function() {
					uni.navigateTo({
						url: _successUrl
					})
				});
				// #endif

				return;
			}
			if (res.statusCode == 200 && res.data.code == '100') {
				let data = res.data; //成功情况下跳转
				uni.showToast({
					title: "支付成功",
					duration: 2000
				});
				setTimeout(function() {
					uni.navigateTo({
						url: _successUrl
					})
				}, 2000)

				return;
			}
			wx.showToast({
				title: "缴费失败"+res.data.msg,
				icon: 'none',
				duration: 2000
			});
			_that.banButton = false;
		},
		fail: function(e) {
			wx.hideLoading();
			wx.showToast({
				title: "服务器异常了",
				icon: 'none',
				duration: 2000
			});
			_that.banButton = false;
		}
	});

}
/**
 * 支付接口
 * @param {Object} _data 业务数据
 * @param {Object} _business 支付业务
 * @param {Object} _shopId 商铺ID
 * @param {Object} _money 支付金额
 */
export function toPay(_data,_business,_shopId,_money,_successUrl){

	_data.tradeType = 'JSAPI';
	_data.business = _business;
	_data.shopId = _shopId;
	_data.successUrl = _successUrl;
	uni.setStorageSync('doing_cashier',_data);
	let _userId = getUserId();
	if(!_userId){
		_userId = "-1";
	}
	if(conf.env == 'DEV'){
		uni.navigateTo({
			url:'/pages/order/testCashier?money='+_money+"&business="+_business+"&shopId="+_shopId+"&cashierUserId="+_userId
		})
		return;
	}
	uni.navigateTo({
		url:'/pages/order/cashier?money='+_money+"&business="+_business+"&shopId="+_shopId+"&cashierUserId="+_userId
	});
}